package uc.org.jkas.app.admin;
import java.util.HashMap;
import java.util.ArrayList;
import uc.org.jkas.model.ApplicationsM;
import org.jkas.utils.Page;
public class Applications extends BackBase
{ ApplicationsM asm=new ApplicationsM();
	public void index()
    {  if(IS_POST())
		{
		}
		else
		{int page=Pint("page");
		 int pagenum=20;
		 Page p=new Page(page,pagenum);
		 ArrayList<HashMap> lists=asm.limit(p.getPos(),pagenum).count().order("appid desc").select();
		 p.setCount(asm.getCount());
		 assign("pageinfo",p.getInfo());
		 assign("lists",lists);
		 display();
		}
	}

	public void view()
	{int appid=Pint("appid");
	 HashMap<String,String> info=asm.getInfo(appid);
			assign("info",info);
			display();
	}

	public void add()
	{if(IS_POST())
		{String res=JSON_RESULT_FAILED;
		  HashMap<String,Object> data=new HashMap<String,Object>();
		  String name=P("name");
		  if(!isEmpty(name))
			{
		      String authkey=P("authkey");
		         if(isEmpty(authkey))
			       {authkey=md5(strc(time(),random(100,999)));
			       }
              HashMap<String,String> keys= rsaGenerateKey();
	          data.put("name",P("name"));
		      data.put("url",P("url"));
		      data.put("description",P("description"));
		      data.put("allowips",P("allowips"));
		      data.put("ip",P("ip"));
		      data.put("authkey",authkey);
			  data.put("prikey",keys.get("privatekey"));
			  data.put("pubkey",keys.get("publickey"));
		      data.put("add_time",time());
		      if(asm.insert(data)>0)
			    {res=JSON_RESULT_SUCCESS;
			    }
			}
			else
			{res="{\"code\":203,\"msg\":\"empty name\"}";
			}
          echo(res);
		}
		else
		{ 
			display();
		}
	}

	public void edit()
	{  int appid=Pint("appid");
		if(IS_POST())
		{ String res=JSON_RESULT_FAILED;
		  HashMap<String,Object> data=new HashMap<String,Object>();
		  String name=P("name");
		  if(!isEmpty(name))
			{
	          String authkey=P("authkey");
		         if(isEmpty(authkey))
			       {authkey=md5(strc(time(),random(100,999)));
			       }
               boolean resetkeys=Pint("resetkeys")==1;
		       if(resetkeys)
			   {HashMap<String,String> keys= rsaGenerateKey();
			    data.put("prikey",keys.get("privatekey"));
			    data.put("pubkey",keys.get("publickey"));
			   }
			    data.put("keysverify",Pint("keysverify"));
	            data.put("name",name);
		        data.put("url",P("url"));
		        data.put("description",P("description"));
		        data.put("allowips",P("allowips"));
		        data.put("ip",P("ip"));
		        data.put("authkey",authkey);
		        if(asm.update(strc("appid=",appid),data)>0)
			     {res=JSON_RESULT_SUCCESS;
			     }
			}
			else
			{res="{\"code\":203,\"msg\":\"empty name\"}";
			}
          echo(res);
		}
		else
		{   
			HashMap<String,String> info=asm.getInfo(appid);
			assign("info",info);
			display();
		}
	}

	public void drop()
	{  String res=JSON_RESULT_FAILED;
	  if(IS_POST())
		{
		 int appid=Pint("appid");
	     if(asm.dropInfo(appid)>0)
		 {res=JSON_RESULT_SUCCESS;
		 }
		}
		echo(res);
	}

	public void downpemkey()
	{int appid=Pint("appid");
	 HashMap<String,String> info=asm.getInfo(appid);
	 header("Content-Type","application/x-x509-ca-cert");
	 header("Content-Disposition", strc("attachment; filename=\"prikey_",appid,".pem\""));
     echo(rsaPrikey2pem(info.get("prikey")));
	}

}